//
//  SingleController.m
//  Kugou-OC
//
//  Created by BH on 16/5/26.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "SingleController.h"
#import "ExpendCommonCell.h"
#import "GlobalDefine.h"
#import "MusicInfoModel.h"

@interface SingleController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *singleTable;
//选中行
@property(nonatomic,strong)NSIndexPath *selectedIndexPath;

@end

@implementation SingleController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    _singleTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _singleTable.dataSource = self;
    _singleTable.delegate = self;
    _singleTable.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_singleTable];
}

#pragma mark - UITableView datasource & delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _singleList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ExpendCommonCell *cell = [[ExpendCommonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    cell.titleLab.text = [_singleList[indexPath.row] music_name];
    cell.subtitleLab.text = [_singleList[indexPath.row] music_artist];
    [cell.rightBtn addTarget:self action:@selector(expendCell:) forControlEvents:UIControlEventTouchUpInside];
    cell.itemsDict = @{@"localmusic_soundquality":@"升级音质",@"localmusic_add":@"添加",@"localmusic_share":@"分享",@"localmusic_singinfo":@"歌曲信息",@"localmusic_delete":@"删除",@"localmusic_roaming":@"歌曲漫游",@"localmusic_k":@"K歌",@"localmusic_mv":@"MV"};
    
    if (indexPath == _selectedIndexPath) {
        [cell addSubviewWithOpen:YES];

    }else{
        [cell addSubviewWithOpen:NO];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath == _selectedIndexPath) {
        return 180;
    }
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

-(void)expendCell:(id)sender{
    
    NSIndexPath *indexPath = [self.singleTable indexPathForCell:((ExpendCommonCell*)[[sender superview]superview])];
    
    [self.singleTable beginUpdates];
    if(_selectedIndexPath==nil)
    {
        _selectedIndexPath=indexPath;
        [self.singleTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    else{
        BOOL hasSelectedOtherRow = ![self.selectedIndexPath isEqual:indexPath];
        
        NSIndexPath *temp=self.selectedIndexPath;
        self.selectedIndexPath=nil;
        [self.singleTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:temp] withRowAnimation:UITableViewRowAnimationAutomatic];
        if(hasSelectedOtherRow){
            self.selectedIndexPath=indexPath;
            [self.singleTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }
    [self.singleTable endUpdates];
}

@end
